﻿#ifndef WIDGET_H
#define WIDGET_H

#include "send.h"
#include "utils/datamanager.h"
#include <QWidget>
#include <QTableWidgetItem>
#include <QTcpSocket>
#include <QMessageBox>
#include <QTcpSocket>
#include <QLineEdit>
#include <QPair>
#include <QSql>
#include <QSqlDatabase>
#include <QSqlQuery>

//class QSqlDatabase;
class GroupItemData;
class QLabel;
class FormSetting;
class QSystemTrayIcon;
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
public:
    static void stylize(QString classname, QWidget *widget);

    enum em_PutStatus{
        emPS_Not   =    1,
        emPS_Pause,
        emPS_Start,
        emPS_Playing,
        emPS_Stop
    };

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    //create by vacant

    //end

private:
    Ui::Widget *ui;

    FormSetting *settingform;
    QMenu *menuImport;

    QTableWidgetItem *item;
    QSqlDatabase db;
    QByteArray recv_buf;//接收缓冲区
    struct send_head buf;
    QLineEdit *lineEdit;
    QTcpSocket *client_tcp;
    QPushButton *preBtnPut;

    //create by vacant
    QTimer *TimerGlbDly;
//    QHash<int,QMap<>> _HashIpAddList;
    QMap<int,QString> _GroupIpList;
    send_head _CurrentGrpSHData;
    QMap<int/*组号*/,QPair<QPair<sendHead/*称量*/,sendHead/*实际称量*/>,bool/*状态-已放料还是未放料状态*/>> _BarreStatus;//存储各组相应桶号称量
    QMap<int,sendHead> m_BarrelStatus;
    QHash<int,QTcpSocket*> tcpSocketHash;

    QPair<QStringList,bool> m_glbBoxnum;
    int _DlyGlobalTime;//全局-延时计时值变量-用作定时器定时
    int _DlyGlobalTimeS;//全局-延时计时值变量-用作定时器定时
    bool flag,_flagAppStartFirst,IsImportDataAuto;
    bool flagSortTable;

    void defaultSetting();
    void loadSetting();
    void saveSetting();
    void updateDlyTimeSave();
    void updateDlyTime(int groupid, QDateTime datetime);
    QSystemTrayIcon *system_tray;
    FormSetting *setupSettingForm(QWidget *parent);
    QPoint offset;

    //end

    bool err[24];

    void initWndInfo(QWidget *wnd, GroupItemData itemdata);
    //modify & add by vacant
    void set_ui();// UI设置函数
    void SetWidgetGrowEffect(QWidget *widget);
    void initHomeBtnEffect();
    void initHomeButton();
    void initTreeWidget();

    void groupButtonStatus(QString group,em_PutStatus state);

    quint16 GetCheckSum (void *data, quint16 len);//校验函数
    //end.

    void insert_data(int num, QString Formula, float lg,QString flag);//数据出入函数

//create by vacant:2014-08-17
signals:
    void onCloseWidget();//退出程序信号

    void sendCustomDlyTime(QDateTime);

    void sendConnectSocketReturn(int,bool);//test

    void sendConnectSocket(QMap<int,QString>);//点击“发送”按钮,触发信号,发送组号及对应IP
    void sendGroupPutWeithInfo(int,send_head);//发送称量信息-以得到返回称量并进行UI交互
    void sendGroupBatchInfo(int);//发送投料信息-以进行最后实际处理-TCP接收站信号后发送数据到硬件进行处理
    void closeConnect();//断开连接信号

    void updateGroupData();

    void calcTimFinished();

    void sigChangeBoxSort(QStringList);

    void sig_resize(QResizeEvent*);
//end!
private slots:
    void on_pushButton_weigh_clicked();
    bool connectDB();//连接数据库函数
    void on_pushButton_link_clicked();
    void on_pushButton_put_clicked();

    ///------------------------------------------------old Data
//    void on_pushButton_close_clicked();
    void on_pushButton_getinfo_clicked();
    void write_xls();//导出xls
    bool cheack_value(QString str);//检查输入的值的格式
    void Weigh_item_cheack(QTableWidgetItem * item);//
    void display();
    void recv_data(QTcpSocket *client_tcp = NULL);
    void connect_Successful();
    void connect_Close();
    ///------------------------------------------------old Data end!
    //create by vacant

    void sltcbGrpChange(const QString &text);
    void sltcbTblCellChange(int row,int column);
    void setupTableItem(QStringList boxnumList = QStringList());//每一列数据值初始化

    void setupDelayPutWetTime();//对话框:设置定时放料
    void startPutOperate(int groupid = -1);//开始放料操作函数

    //import data from excel
    void ImportData(bool IsAuto = false);
    void ImportDataFromThreadFinished(int groupnum, QMap<int, QString> map_xls, QStringList boxnum = QStringList());
    void ExportDataToExcelExists(int group);//导出到已有excel表中

    //Put Weigh Finish and record to sql data.
    void RecordToSql(int groupNum = -1 );

    void finishedConnectSlot(int /*group number*/,bool /*connect state*/);//返回对应组号连接状态,true表示连接成功
    void finishedPutWeigh(int group, send_head realput);//称量成功后返回实际称量对应槽处理函数

    void checkItemValue();

    void sltChangeBoxSort(QStringList strlist);

    void copyTable(int column = 2);
    void on_switchWin_currentChanged(int arg1);

protected:
    void closeEvent(QCloseEvent *event);
//    void keyReleaseEvent(QKeyEvent *event);
    void resizeEvent(QResizeEvent *event);
    bool eventFilter(QObject *, QEvent *);


    void mousePressEvent(QMouseEvent *event);

    void mouseMoveEvent(QMouseEvent *event);

    void mouseReleaseEvent(QMouseEvent *event);
    //end
};

#endif // WIDGET_H
